(function() {

	var defaultUserPic = "../img/defaultUserPic.png";
	$("#UserPic").error(function() {
		$(this).attr("src", defaultUserPic);
		$(this).error = null;
	});

	$(document).ready(function() {
		getContent();
	});

	function getContent() {
		var hierarchy = jQuery('#hierarchy').val();
		var url = window.rootPath + '/ui/mailList/findHierarchys/' + hierarchy;
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "get",
			data : "",
			dataType : "json",
			contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
			success : function(data) {
				var hierarchys = data.hierarchys;
				var users = data.users;
				setHierarchyListView(hierarchys);
				setUserListView(users);
			}
		})
	}

	function setHierarchyListView(pageInfo) {
		$("#hierarchyList").empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无组织信息</h2></li>';
			$("#hierarchyList").append(contenthtml);
			$("#hierarchyList").listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var contenthtml = '<li class="touch"><div class="item">'
					+ '<div class="left-view libg' + i % 2 + '">'
					+ '<span><font>' + pageInfo[i].groupName + '</font></span>'
					+ '</div>' + '<div class="right-view">';
//			if (pageInfo[i].isNext > 0) {
//				contenthtml += '<span class="next" defineId="'
//						+ pageInfo[i].id
//						+ '"><font>下级</font></span><span class="info" defineId="'
//						+ pageInfo[i].id + '"><font>详情</font></span>';
//			} else {
//				contenthtml += '<span class="info infoOnly"  defineId="'
//					+ pageInfo[i].id + '"><font>详情</font></span>';
//			}
			contenthtml += '<span class="next" defineId="'
			+ pageInfo[i].id
			+ '"><font></font></span>'
//			+'<span class="info" defineId="'
//			+ pageInfo[i].id + '"><font>详情</font></span>'
			;
			contenthtml += '</div>' + '</div></li>';
			$("#hierarchyList").append(contenthtml);
		}
		$("#hierarchyList").listview('refresh');
		for (var i = 0; i < pageInfo.length; i++) {
			$("#" + pageInfo[i].id).removeClass("ui-btn-icon-right");
		}

	}

	function setHierarchyListView2(pageInfo) {
		$("#hierarchyList2").empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无组织信息</h2></li>';
			$("#hierarchyList2").append(contenthtml);
			$("#hierarchyList2").listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var contenthtml = '<li class="touch"><div class="item">'
				+ '<div class="left-view libg' + i % 2 + '">'
				+ '<span><font>' + pageInfo[i].groupName + '</font></span>'
				+ '</div>' + '<div class="right-view">';
//			if (pageInfo[i].isNext > 0) {
//				contenthtml += '<span class="next" defineId="'
//						+ pageInfo[i].id
//						+ '"><font>下级</font></span><span class="info" defineId="'
//						+ pageInfo[i].id + '"><font>详情</font></span>';
//			} else {
//				contenthtml += '<span class="info infoOnly"  defineId="'
//					+ pageInfo[i].id + '"><font>详情</font></span>';
//			}
			contenthtml += '<span class="next" defineId="'
				+ pageInfo[i].id
				+ '"><font></font></span>'
//			+'<span class="info" defineId="'
//			+ pageInfo[i].id + '"><font>详情</font></span>'
				;
			contenthtml += '</div>' + '</div></li>';
			$("#hierarchyList2").append(contenthtml);
		}
		$("#hierarchyList2").listview('refresh');
		for (var i = 0; i < pageInfo.length; i++) {
			$("#" + pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
		
	}

	function setUserListView(pageInfo) {
		$("#userList").empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无人员信息</h2></li>';
			$("#userList").append(contenthtml);
			$("#userList").listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var contenthtml = '<li class="touch"><div class="item">'
					+ '<div class="left-view libg' + i % 2 + '">'
					+ '<span><font>' + pageInfo[i].name + '</font></span>'
					+ '</div>' + '<div class="right-view">'
					+ '<span class="info infoOnly" defineId="' + pageInfo[i].id
					+ '"><font></font></span>' + '</div>' + '</div></li>';
			$("#userList").append(contenthtml);
		}
		$("#userList").listview('refresh');
		for (var i = 0; i < pageInfo.length; i++) {
			$("#" + pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}

	function setUserListView2(pageInfo) {
		$("#userList2").empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无人员信息</h2></li>';
			$("#userList2").append(contenthtml);
			$("#userList2").listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var contenthtml = '<li class="touch"><div class="item">'
				+ '<div class="left-view libg' + i % 2 + '">'
				+ '<span><font>' + pageInfo[i].name + '</font></span>'
				+ '</div>' + '<div class="right-view">'
				+ '<span class="info infoOnly" defineId="' + pageInfo[i].id
				+ '"><font></font></span>' + '</div>' + '</div></li>';
			$("#userList2").append(contenthtml);
		}
		$("#userList2").listview('refresh');
		for (var i = 0; i < pageInfo.length; i++) {
			$("#" + pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}

	//取消右滑特效
//	
//	$("#hierarchyList").on("swipeleft", ".item", function() {
//		$("#hierarchyList").each(function() {
//			$(this).find('.item').removeClass('selected');
//		});
//		$(this).addClass('selected');
//	});
//
//	$("#hierarchyList").on("swiperight", ".item", function() {
//		$(this).removeClass('selected');
//	});
//
//	$("#userList").on("swipeleft", ".item", function() {
//		$("#userList").each(function() {
//			$(this).find('.item').removeClass('selected');
//		});
//		$(this).addClass('selected');
//	});
//
//	$("#userList").on("swiperight", ".item", function() {
//		$(this).removeClass('selected');
//	});

	$("#hierarchyList").on(
			"click",
			".next",
			function() {
				var id = $(this).attr("defineId");
				window.location.href = window.rootPath
						+ "/ui/mailList?device=mobile&identify=&hierarchy="
						+ id;
			});

	$("#hierarchyList").on("click", ".info", function() {
		$("#hierarchyList").each(function() {
			$(this).find('.item').removeClass('selected');
		});
		var id = $(this).attr("defineId");
		var url = window.rootPath + '/ui/mailList/findHierarchyInfon/' + id;
		jQuery.ajax({
			url : encodeURI(url),
			type : "post",
			dataType : "json",
			data : '',
			contentType : "application/json;charset=UTF-8",
			success : function(data) {
				cleanHierarchy();
				jQuery('#groupName').text(data.hierarchy.groupName);
				var role = $("#roleName").val();

				if (role == '游客') {
					phone = '无查看权限';
				} else {
					phone = data.hierarchy.groupTelphone;
				}
				jQuery('#groupTelphone').text(phone);
				jQuery('#memo').text(data.hierarchy.memo);
				$.mobile.changePage('#hierarchyPage');
			}
		});

	});

	function cleanHierarchy() {
		jQuery('#groupName').text('');
		jQuery('#groupTelphone').text('');
		jQuery('#memo').text('');
	}

	$("#userList").on("click", ".info", function() {
		$("#userList").each(function() {
			$(this).find('.item').removeClass('selected');
		});
		var id = $(this).attr("defineId");
		var url = window.rootPath + '/ui/mailList/findUserInfon/' + id;
		jQuery.ajax({
			url : encodeURI(url),
			type : "post",
			dataType : "json",
			data : '',
			contentType : "application/json;charset=UTF-8",
			success : function(data) {
				cleanUser();
				var userPic = data.user.pictureAddress;
				if (userPic == undefined || userPic == "") {
					userPic = defaultUserPic;
				} else {
					userPic = window.rootPath + '/' + userPic;
				}
				$("#UserPic").attr("src", userPic);

				jQuery('#name').text(data.user.name);
				jQuery('#sex').text(data.user.sex);
				var role = $("#roleName").val();
				var phone = '';
				if (role == '游客') {
					phone = '无查看权限';
				} else {
					phone = data.user.contactType;
				}
				jQuery('#contactType').html("<a href='tel:"+phone+"'>"+phone+"</a>");
				jQuery('#email').text(data.user.email);
				jQuery('#memo').text(data.user.memo);
				$.mobile.changePage('#userPage');
			}
		});

	});

	function cleanUser() {
		jQuery('#name').text('');
		jQuery('#sex').text('');
		jQuery('#contactType').text('');
		jQuery('#email').text('');
		jQuery('#memo').text('');
	}

	$("body").on(
			"click",
			".goBackMailList",
			function() {
				var id = jQuery('#back').val();
				window.location.href = window.rootPath
						+ "/ui/mailList?device=mobile&identify=&hierarchy="
						+ id;
			});
	
	$("body").on('click','#searchMain',function(){
		$('#search').val('');
		
//		$("#hierarchyList2").empty();
//		var contenthtml = '<li><h2 style="color:blue">暂无组织信息</h2></li>';
//		$("#hierarchyList2").append(contenthtml);
		
		$("#userList2").empty();
		var contenthtml = '<li><h2 style="color:blue">暂无人员信息</h2></li>';
		$("#userList2").append(contenthtml);

		
		$.mobile.changePage('#searchPage');
	})
	
//	$(document).on('input propertychange','#search',function (e) {
//	    if (e.type === "input" || e.orignalEvent.propertyName === "value") {
//	        console.log(this.value);
//	        getSearch(this.value);
//	    }
//	})
	
	$("body").on('click','#searchBtn',function(){
		if($('#search').val().trim() == ''){
			$("#userList2").empty();
			var contenthtml = '<li><h2 style="color:blue">暂无人员信息</h2></li>';
			$("#userList2").append(contenthtml);
		}else{
			getSearch($('#search').val());
		}
	})
	
	function getSearch(value){
		var key = {
			value : value
		}
		var url = window.rootPath + '/ui/mailList/findSearch';
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "post",
			data : key,
			dataType : "json",
			success : function(data) {
//				var hierarchys = data.hierarchys;
				var users = data.users;
//				setHierarchyListView2(hierarchys);
				setUserListView2(users);
			}
		})
	}
	
	$("#userList2").on("click", ".info", function() {
		$("#userList2").each(function() {
			$(this).find('.item').removeClass('selected');
		});
		var id = $(this).attr("defineId");
		var url = window.rootPath + '/ui/mailList/findUserInfon/' + id;
		jQuery.ajax({
			url : encodeURI(url),
			type : "post",
			dataType : "json",
			data : '',
			contentType : "application/json;charset=UTF-8",
			success : function(data) {
				cleanUser2();
				var userPic = data.user.pictureAddress;
				if (userPic == undefined || userPic == "") {
					userPic = defaultUserPic;
				} else {
					userPic = window.rootPath + '/' + userPic;
				}
				$("#UserPic2").attr("src", userPic);

				jQuery('#name2').text(data.user.name);
				jQuery('#sex2').text(data.user.sex);
				var role = $("#roleName").val();
				var phone = '';
				if (role == '游客') {
					phone = '无查看权限';
					jQuery('#contactType2').html(phone);
				} else {
					phone = data.user.contactType;
					jQuery('#contactType2').html("<a href='tel:"+phone+"'>"+phone+"</a>");
				}
				
				jQuery('#email2').text(data.user.email);
				jQuery('#memo2').text(data.user.memo);
				$.mobile.changePage('#userPage2');
			}
		});

	});
	
	function cleanUser2() {
		jQuery('#name2').text('');
		jQuery('#sex2').text('');
		jQuery('#contactType2').text('');
		jQuery('#email2').text('');
		jQuery('#memo2').text('');
	}
	
	$("#hierarchyList2").on("click",".next",function() {
		var id = $(this).attr("defineId");
		getSearchNext(id);
	});
	
	function getSearchNext(value){
		var url = window.rootPath + '/ui/mailList/findHierarchys/'+value;
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "post",
			data : '',
			dataType : "json",
			success : function(data) {
				var hierarchys = data.hierarchys;
				var users = data.users;
				setHierarchyListView2(hierarchys);
				setUserListView2(users);
			}
		})
	}
	
	

})();